import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.concurrent.Callable;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 牛孟俊
 * Date: 2023-11-16
 * Time: 18:11
 */
public class ReflectClassDemo {

    //reflectNewInstance这个方法用来帮助我们创建对象
    public static void reflectNewInstance() throws ClassNotFoundException,
            InstantiationException, IllegalAccessException {
        Class<?> c1 = Class.forName("Student");//获取Class对象

        //通过反射穿件一个实例

        Student student = (Student) c1.newInstance();
        //c1.newInstance()返回的是一个object的
        System.out.println(student);
    }

    //反射私有属性
    public static void reflectPrivateField() {

        try {
            Class<?> c1 = Class.forName("Student");

            Student student = (Student) c1.newInstance();
            //创建类的实例

            Field field = c1.getDeclaredField("name");
            //获取的是name这个私有属性

            field.setAccessible(true);
            //修改为true，就可以进行修改了

            //开始修改
            field.set(student,"PUBG");

            System.out.println(student);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }  catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (NoSuchFieldException e) {
            throw new RuntimeException(e);
        }


    }
    public static void main2(String[] args) {
        reflectPrivateField();
    }
    //反射私有构造方法
    public static void reflectPrivateConstructor() {
        try {
            Class<?> c1 = Class.forName("Student");
            Constructor<?> constructor =  c1.getDeclaredConstructor(String.class,int.class);

            constructor.setAccessible(true);

            Student student = (Student)constructor.newInstance("mengjun",22);
            System.out.println(student);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    public static void main3(String[] args) {
        reflectPrivateConstructor();
    }

    //反射私有方法
    public static void reflectPrivateMethod() {
        try {
            Class<?> c1 = Class.forName("Student");
            Student student =(Student)c1.newInstance();

            Method method = c1.getDeclaredMethod("function", String.class);
            method.setAccessible(true);
            method.invoke(student,"我是一个参数");

        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }

    }

    public static void main(String[] args) {
        reflectPrivateMethod();

    }
    public static void main1(String[] args) {
        try {
            reflectNewInstance();
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

}
